
setwd("/Users/sergimartinez/Dropbox/EUI/BET HOUSES PROJECT/data/estimates_050121/")
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data")

####### Setting the theme:

## This was used for the first map:
library(haven)
library(ggmap)
library(ggplot2)
library(sp)
library(sf)
apuestas <- read_dta("apuestas_tipos_.dta") # all betting shops
escuelas <- read_dta("colegios2_.dta") # all schools
escuelas_pu <- read_dta("colegios3_.dta") # all PUBLIC schools
escuelas_priv <- read_dta("colegios4_.dta") # all charter/PRIVATE schools
allmap <- read_dta("all_mapa2.dta") 

apuestas2 <- apuestas %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

escuelas1 <- escuelas %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

escuelas2 <- escuelas_pu %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

escuelas3 <- escuelas_priv %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

all2 <- allmap %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

## The following graphs are the ones created for the PNAS submission:

all2$id <- factor(all2$tipo,
                  levels = c(1,2,3),
                  labels = c("Betting houses", "Public high-schools", "Charter high-schools"))
library(extrafont)
font_import()
loadfonts(device="win")

### The good one that goes into the PNAS:
library(tmaptools)
library(haven)
library(ggmap)
library(sp)
library(sf)


bbox <- c(-3.801089, 40.347347, -3.577063, 40.513743)

palette2 = c("red", "blue", "black")

figureS2 <- ggmap(get_stamenmap(bbox, zoom = 13),darken = c(0.5, "white")) +
  geom_sf(data=all2,
          inherit.aes =FALSE,
          aes(color = id, shape = id),
          alpha=1,
          size=3.5)+
  guides(color=guide_legend(override.aes=list(fill=NA)))+
  scale_shape_manual(values=c(3,1,1)) + 
  scale_color_manual(values = palette2)+
  labs(x="",y="",
       caption = "Note: Map created by the authors using opendata from Madrid City Council.\nMap tiles by Stamen Design, under CC BY 3.0.")+
  #theme_results +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        #plot.margin = unit(c(0, 0, -1, -1), 'lines'),
        plot.title = element_text(hjust = .5,
                                  family="Times New Roman",
                                  size=18),
        legend.position = "top",
        legend.key = element_rect(fill = NA),
        legend.title = element_blank(),
        text=element_text(family="Times",size = 14))
figureS2
#ggsave(file="mapa_060121.png", plot=map1, width = 20, height = 20, units = "cm") 
#ggsave(file="mapa_070721.png", plot=map1, width = 20, height = 20, units = "cm") 


